﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameRoomInfoSetPage : EventManager {

    public override void InformationSetting()
    {
        appendTime = 0;
        AppendTimeSliderSelect(0);
        modifiScore = GameRoomModel.GetInstance().MaxTakeInScore;
        ModifiScoreSliderSelect(GetNumFromModifiScore(GameRoomModel.GetInstance().MaxTakeInScore));
        modifiScoreSlider.value = GetNumFromModifiScore(GameRoomModel.GetInstance().MaxTakeInScore) * 0.25f;

        AddListenerToButton(confirmBtn, ConfirmBtnClick);
        AddListenerToButton(cancelBtn, CancelBtnClick);
    }

    public Button confirmBtn;
    public Button cancelBtn;
    public Slider addTimeSlider;
    public GameObject addTimeNumGroup;
    public Slider modifiScoreSlider;
    public GameObject modifiScoreNumGroup;

    private int appendTime;
    public void AppendTimeSliderValueChange()
    {
        int temp = Mathf.FloorToInt(addTimeSlider.value / 0.25f);
        addTimeSlider.value = temp * 0.25f;
        GetAppendTime(temp.ToString());
        AppendTimeSliderSelect(temp);
    }

    private void AppendTimeSliderSelect(int num)
    {
        for (int i = 0; i < 5; i++)
        {
            if (i == num)
            {
                addTimeNumGroup.transform.GetChild(i).gameObject.SetActive(true);
            }
            else
            {
                addTimeNumGroup.transform.GetChild(i).gameObject.SetActive(false);
            }
        }

        for (int i = 5; i < 10; i++)
        {
            if (i == num + 5)
            {
                addTimeNumGroup.transform.GetChild(i).gameObject.SetActive(false);
            }
            else
            {
                addTimeNumGroup.transform.GetChild(i).gameObject.SetActive(true);
            }
        }
    }

    private void GetAppendTime(string str)
    {
        switch (str)
        {
            case "0":
                appendTime = 0;
                break;
            case "1":
                appendTime = 30;
                break;
            case "2":
                appendTime = 60;
                break;
            case "3":
                appendTime = 120;
                break;
            case "4":
                appendTime = 180;
                break;
        }
    }


    private int modifiScore;
    public void modifiScoreSliderValueChange()
    {
        int temp = Mathf.FloorToInt(modifiScoreSlider.value / 0.25f);
        modifiScoreSlider.value = temp * 0.25f;
        GetModifiScore(temp.ToString());
        ModifiScoreSliderSelect(temp);
    }

    private void ModifiScoreSliderSelect(int num)
    {
        for (int i = 0; i < 5; i++)
        {
            if (i == num)
            {
                modifiScoreNumGroup.transform.GetChild(i).gameObject.SetActive(true);
            }
            else
            {
                modifiScoreNumGroup.transform.GetChild(i).gameObject.SetActive(false);
            }
        }

        for (int i = 5; i < 10; i++)
        {
            if (i == num + 5)
            {
                modifiScoreNumGroup.transform.GetChild(i).gameObject.SetActive(false);
            }
            else
            {
                modifiScoreNumGroup.transform.GetChild(i).gameObject.SetActive(true);
            }
        }
    }


    private int  GetNumFromModifiScore(int score)
    {
        int num = 0;
        switch (score)
        {
            case 1:
                num = 0;
                break;
            case 2:
                num = 1;
                break;
            case 4:
                num = 2;
                break;
            case 6:
                num = 3;
                break;
            case 8:
                num = 4;
                break;
        }
        return num;
    }

    private void GetModifiScore(string str)
    {
        switch (str)
        {
            case "0":
                modifiScore = 1;
                break;
            case "1":
                modifiScore = 2;
                break;
            case "2":
                modifiScore = 4;
                break;
            case "3":
                modifiScore = 6;
                break;
            case "4":
                modifiScore = 8;
                break;
        }
    }


    public void ConfirmBtnClick()
    {
        if (appendTime == 0 && modifiScore == GameRoomModel.GetInstance().MaxTakeInScore)
        {
            TooL.ShowNoticePage("请输入有效的设置！");
        }
        else
        {
            MessageManager.GetInstance().SendMsg1239(1, 0, modifiScore, appendTime, GameRoomModel.GetInstance().RoomID);
            CloseBtnClick();
        }     
    }

    public void CancelBtnClick()
    {
        CloseBtnClick();
    }
}
